package com.lst.servlet;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.entity.request.ClientDiscussReqPara;
import com.lst.entity.response.ClientDiscussResPara;
import com.lst.model.ClientNum;
import com.lst.model.MstDiscuss;
import com.lst.service.ClientNumService;
import com.lst.service.MstDiscussService;
import com.lst.utils.CommFunUtils;
import com.lst.utils.RequestUtils;

/**
 * @ClassName: DiscussListServlet
 * @Description: 评论列表接口
 * @author zmm
 * @date 2017年11月17日 下午3:32:01
 *
 */
public class DiscussListServlet extends BaseServlet {


	private static final long serialVersionUID = 1L;

	private MstDiscussService mstDiscussService = ctx.getBean(MstDiscussService.class); 

	private ClientNumService clientNumService = ctx.getBean(ClientNumService.class);

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {

		ClientDiscussResPara resPara = new ClientDiscussResPara();

		try {
			ClientDiscussReqPara reqPara = getReqPara(request);

			if(isCheckField(reqPara,resPara)){
				Integer tarid = Integer.valueOf(reqPara.getTarid());
				Integer page = Integer.valueOf(reqPara.getPage());
				Integer pagesize = Integer.valueOf(reqPara.getPagesize());
				String clientid = reqPara.getUserid(); 
				Integer type = Integer.valueOf(reqPara.getType());
				
				Map<String, Object> dymap = new HashMap<String, Object>();
				dymap.put("enabled", true);
				dymap.put("isDeleted", false);
				dymap.put("tarid",tarid );
				dymap.put("type", type);
	            //评论数量
				Integer discussNum = mstDiscussService.queryCountByMap(dymap);
				resPara.setDiscussNum(discussNum);

				//查询一级评论
				Map<String, Object> reqMap = new HashMap<String, Object>();		

				reqMap.put("enabled", true);
				reqMap.put("isDeleted", false);
				reqMap.put("parentId", "main");
				reqMap.put("tarId", tarid);
				reqMap.put("type", type);
				reqMap.put("orderByClause", "md.createdate desc");

				PageBounds pageBounds = new PageBounds(page,pagesize);	

				List<MstDiscuss> mainList = mstDiscussService.queryByList(reqMap, pageBounds);
				if(mainList!=null&&mainList.size()>0){
					for(MstDiscuss discuss:mainList){
						Date date=discuss.getCreatedate();
	  					Long createdate2=date.getTime();
	  					discuss.setCreatedate2(createdate2);
					}
				}

				PageBounds noPage = new PageBounds();	

				//查询上级评论
				Map<String, Object> tempMap = new HashMap<String, Object>();
				if(mainList!=null&&mainList.size()>0){
					for(MstDiscuss md : mainList){

						//点赞量
						Integer praisenum = CommFunUtils.opNum(null, md.getId(), CommCode.SYSTEM_CARESEL_RANGE_DISCUSS, CommCode.SYSTEM_COUNT_PRAISE, clientNumService);

						md.setPraisenum(praisenum);

						tempMap.put("enabled", true);
						tempMap.put("isDeleted", false);
					//	tempMap.put("rootId", md.getId());
						tempMap.put("tarId", tarid);
						tempMap.put("type", type);
						tempMap.put("id", md.getParentid());
						tempMap.put("orderByClause", "md.createdate desc");

						List<MstDiscuss> autoList = mstDiscussService.queryByList(tempMap, noPage);
						md.setMds(autoList);

						//点赞
						List<ClientNum> cns = null;

						if(!StringUtils.isEmpty(clientid)){
							Map<String, Object> map = new HashMap<String, Object>();

							map.put("enabled", true);
							map.put("isDeleted", false);
							map.put("clientid", Integer.valueOf(clientid));
							map.put("actid", md.getId());
							map.put("acttype", CommCode.SYSTEM_CARESEL_RANGE_DISCUSS);
							map.put("numtype",CommCode.SYSTEM_COUNT_PRAISE);

							cns = clientNumService.queryByList(map);
						}

						if(null==cns||cns.size()==0){
							md.setIsSup(false);					
						}else{
							md.setIsSup(true);					
						}	

					}
				}
				
				
				resPara.setMdlist(mainList);
				resPara.setCode(CommCode.M_Y000000);
				resPara.setMessage(CommCode.M_Y000001);
			}

		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0001);

			logger.error("doPost error: ", e);
		}

		try {
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");

			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
			String jsonResult = gson.toJson(resPara);

	//		logger.info("DiscussListServlet jsonResult: " + jsonResult);

			PrintWriter out = response.getWriter();
			out.print(jsonResult);
			out.flush();
			out.close();

		//	logger.info("doPost end run(s): " + DateUtil.calLastedTime(startDate));
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0003);

			logger.error("doPost error: ", e);
		}
	}

	@Override
	public ClientDiscussReqPara getReqPara(HttpServletRequest request) {

		ClientDiscussReqPara reqPara = null;

		try {
			BaseRequest baseRequest = RequestUtils.getRequestPara(request, new ClientDiscussReqPara());

			reqPara = (ClientDiscussReqPara) baseRequest.clone();

			String tarid = request.getParameter("tarid");
			String page = request.getParameter("page");
			String pagesize = request.getParameter("pagesize");
			String userid = request.getParameter("userid");
			String type = request.getParameter("type");
			
//			logger.info("reqPara tarid: " + tarid);
//			logger.info("reqPara page: " + page);
//			logger.info("reqPara pagesize: " + pagesize);
//			logger.info("reqPara userid: " + userid);
//			logger.info("reqPara type: " + type);

			reqPara.setTarid(tarid);
			reqPara.setPage(page);
			reqPara.setPagesize(pagesize);
			reqPara.setUserid(userid);
			reqPara.setType(type);

		} catch (Exception e) {
			logger.error("getReqPara error: ", e);
		}

		return reqPara;
	}

	@Override
	public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
		boolean flag = false;

		ClientDiscussReqPara cdReqPara = (ClientDiscussReqPara) reqPara;
		ClientDiscussResPara cdResPara = (ClientDiscussResPara) resPara;

		if (StringUtils.isEmpty(cdReqPara.getTarid())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage(CommCode.M_B000001);		
		} else if (StringUtils.isEmpty(cdReqPara.getPage())){
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage(CommCode.M_P000009);
		} else if (StringUtils.isEmpty(cdReqPara.getPagesize())){
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage(CommCode.M_P000010);
		} else if (StringUtils.isEmpty(cdReqPara.getType())){
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage(CommCode.M_B000003);
		} else {
			flag = true;
		}

		return  flag;
	}
}
